var express = require('express');
var router = express.Router();
var sql = require('./../sql');
var Product = require('./../sql/col/products');
var Category = require('./../sql/col/categorys');
router.get('/', function(req, res, next) {
  res.send('分类相关接口');
});
/**
 * @api {get} api/category/getCategory 获取分类
 * @apiDescription 获取分类
 * @apiGroup category
 * @apiSuccessExample { json } Success-Response:
 * res.send({
      code: '200',
      message: '获取分类',
      data
    });
 * @apiSampleRequest /api/category/getCategory
 * @apiVersion 1.0.0
 */
router.get('/getCategory', function(req, res, next) {
  // 1.查询产品的数据库，检索出所有的产品的分类，然后去重（---只适用于我们现在的项目----真实项目中可能有专门的分类数据库
  sql.find(Category, {}, { _id: 0 }).then(data => {
    res.send({
      code: '200',
      message: '获取分类',
      data
    });
  })
});


/**
 * @api {get} api/category/flower/girlfriend 鲜花分类送女友列表数据
 * @apiDescription 分类列表
 * @apiGroup category
 * @apiSuccessExample { json } Success-Response:
 * res.send({
      code: '200',
      message: '分类列表',
      len: 0,
      data
    })
 * @apiSampleRequest /api/category/flower/girlfriend
 * @apiVersion 1.0.0
 */
router.get('/flower/girlfriend', function(req, res, next) {
  var reg = new RegExp(`女`, 'i');
  var reg1 = new RegExp(`她`, 'i');
  var reg2 = new RegExp(`你`, 'i');
  // $regex用来设置匹配字符串的正则表达式
  var keyObj = [ 
    { product_info: { $regex: reg} },
    { product_dsc: { $regex: reg} },
    { tag: { $regex: reg} },
    { says: { $regex: reg} },
    { product_info: { $regex: reg1} },
    { product_dsc: { $regex: reg1} },
    { tag: { $regex: reg1} },
    { says: { $regex: reg1} },
    { product_info: { $regex: reg2} },
    { product_dsc: { $regex: reg2} },
    { tag: { $regex: reg2} },
    { says: { $regex: reg2} }
  ]
  sql.find(Product, { type: 'flower', $or: keyObj }, { _id: 0 }).then(data => {
    res.send({
      code: '200',
      message: '分类列表',
      len: data.length,
      data
    });
  })
});


/**
 * @api {get} api/category/flower/boy 鲜花分类送男性列表数据
 * @apiDescription 分类列表
 * @apiGroup category
 * @apiSuccessExample { json } Success-Response:
 * res.send({
      code: '200',
      message: '分类列表',
      len: 0,
      data
    })
 * @apiSampleRequest /api/category/flower/boy
 * @apiVersion 1.0.0
 */
router.get('/flower/boy', function(req, res, next) {
  var reg = new RegExp(`我`, 'i');
  var reg1 = new RegExp(`香槟`, 'i');
  // $regex用来设置匹配字符串的正则表达式
  var keyObj = [ 
    { product_info: { $regex: reg} },
    { product_dsc: { $regex: reg} },
    { tag: { $regex: reg} },
    { says: { $regex: reg} },
    { product_info: { $regex: reg1} },
    { product_dsc: { $regex: reg1} },
    { tag: { $regex: reg1} },
    { says: { $regex: reg1} }
  ]
  sql.find(Product, { type: 'flower', $or: keyObj }, { _id: 0 }).then(data => {
    res.send({
      code: '200',
      message: '分类列表',
      len: data.length,
      data
    });
  })
});


/**
 * @api {get} api/category/flower/elders 鲜花分类送长辈列表数据
 * @apiDescription 分类列表
 * @apiGroup category
 * @apiSuccessExample { json } Success-Response:
 * res.send({
      code: '200',
      message: '分类列表',
      len: 0,
      data
    })
 * @apiSampleRequest /api/category/flower/elders
 * @apiVersion 1.0.0
 */
router.get('/flower/elders', function(req, res, next) {
  var reg = new RegExp(`母`, 'i');
  var reg1 = new RegExp(`康乃馨`, 'i');
  // $regex用来设置匹配字符串的正则表达式
  var keyObj = [ 
    { product_info: { $regex: reg} },
    { product_dsc: { $regex: reg} },
    { tag: { $regex: reg} },
    { says: { $regex: reg} },
    { product_info: { $regex: reg1} },
    { product_dsc: { $regex: reg1} },
    { tag: { $regex: reg1} },
    { says: { $regex: reg1} }
  ]
  sql.find(Product, { type: 'flower', $or: keyObj }, { _id: 0 }).then(data => {
    res.send({
      code: '200',
      message: '分类列表',
      len: data.length,
      data
    });
  })
});

/**
 * @api {get} api/category/flower/friend 鲜花分类送朋友列表数据
 * @apiDescription 分类列表
 * @apiGroup category
 * @apiSuccessExample { json } Success-Response:
 * res.send({
      code: '200',
      message: '分类列表',
      len: 0,
      data
    })
 * @apiSampleRequest /api/category/flower/friend
 * @apiVersion 1.0.0
 */
router.get('/flower/friend', function(req, res, next) {
  var reg = new RegExp(`友`, 'i');
  var reg1 = new RegExp(`百合`, 'i');
  // $regex用来设置匹配字符串的正则表达式
  var keyObj = [ 
    { product_info: { $regex: reg} },
    { product_dsc: { $regex: reg} },
    { tag: { $regex: reg} },
    { says: { $regex: reg} },
    { product_info: { $regex: reg1} },
    { product_dsc: { $regex: reg1} },
    { tag: { $regex: reg1} },
    { says: { $regex: reg1} }
  ]
  sql.find(Product, { type: 'flower', $or: keyObj }, { _id: 0 }).then(data => {
    res.send({
      code: '200',
      message: '分类列表',
      len: data.length,
      data
    });
  })
});

/**
 * @api {get} api/category/flower/material 鲜花材料分类列表数据
 * @apiDescription 分类列表
 * @apiGroup category
 * @apiParam { String } material 分类材料名称
 * @apiSuccessExample { json } Success-Response:
 * res.send({
      code: '200',
      message: '分类列表',
      len: 0,
      data
    })
 * @apiSampleRequest /api/category/flower/material
 * @apiVersion 1.0.0
 */
router.get('/flower/material', function(req, res, next) {
  var { material } = req.query
  var reg = new RegExp(material, 'i');
  // $regex用来设置匹配字符串的正则表达式
  var keyObj = [ 
    { product_info: { $regex: reg} },
    { product_dsc: { $regex: reg} },
    { tag: { $regex: reg} }
  ]
  sql.find(Product, { type: 'flower', $or: keyObj }, { _id: 0 }).then(data => {
    res.send({
      code: '200',
      message: '分类列表',
      len: data.length,
      data
    });
  })
});

/**
 * @api {get} api/category/flower/color 鲜花颜色分类列表数据
 * @apiDescription 分类列表
 * @apiGroup category
 * @apiParam { String } color 分类材料名称
 * @apiSuccessExample { json } Success-Response:
 * res.send({
      code: '200',
      message: '分类列表',
      len: 0,
      data
    })
 * @apiSampleRequest /api/category/flower/color
 * @apiVersion 1.0.0
 */
router.get('/flower/color', function(req, res, next) {
  var { color } = req.query
  var reg = new RegExp(color, 'i');
  // $regex用来设置匹配字符串的正则表达式
  var keyObj = [ 
    { product_info: { $regex: reg} },
    { product_dsc: { $regex: reg} },
    { tag: { $regex: reg} }
  ]
  sql.find(Product, { type: 'flower', $or: keyObj }, { _id: 0 }).then(data => {
    res.send({
      code: '200',
      message: '分类列表',
      len: data.length,
      data
    });
  })
});

module.exports = router;